package com.ht.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ht.entity.TablesEntity;
import com.ht.mapper.TablesMapper;
import com.ht.service.ITablesService;
import org.apache.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ht
 */
@Service
public class TablesServiceImpl extends ServiceImpl<TablesMapper, TablesEntity> implements ITablesService {
    @Override
    public IPage<TablesEntity> queryPage(Page<TablesEntity> page, String key) {
        LambdaQueryWrapper<TablesEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .ne(TablesEntity::getTableSchema, "performance_schema")
                .ne(TablesEntity::getTableSchema, "mysql")
                .ne(TablesEntity::getTableType, "SYSTEM VIEW")
                .ne(TablesEntity::getTableType, "VIEW")
                .and(StringUtils.isNotEmpty(key),andWrapper -> andWrapper.like(StringUtils.isNotEmpty(key), TablesEntity::getTableName, key)
                        .or(StringUtils.isNotEmpty(key),orWrapper -> orWrapper.like(StringUtils.isNotEmpty(key), TablesEntity::getTableSchema, key))
                )
                .orderByAsc(TablesEntity::getTableSchema)
                .orderByAsc(TablesEntity::getTableName)
                .orderByDesc(TablesEntity::getCreateTime);
        return this.baseMapper.selectPage(page, lambdaQueryWrapper);
    }

    @Override
    public TablesEntity queryTable(String tableName) {
        List<TablesEntity> tablesEntities = baseMapper.selectList(new LambdaQueryWrapper<TablesEntity>().eq(TablesEntity::getTableName, tableName));
        if (tablesEntities.size() > 0) {
            return tablesEntities.get(0);
        }
        return null;
    }


}